<?php
// quick return
if (!($_GET['debug'] ?? '')) {
    fastcgi_finish_request();
}

$ip = $_SERVER['REMOTE_ADDR'] ?? '';

if ($ip == '127.0.0.1') { // debug & online
    opcache_reset();
    echo 'Flushed!';
    exit;
} elseif (preg_match('/^192\.168\./', $ip)) { // dev
    $counter = 0;
    $list    = opcache_get_status(true);

    foreach ($list['scripts'] ?? [] as $script => $info) {
        if (!strpos($script, $ip)) {
            continue;
        }
        if (!opcache_is_script_cached($script)) {
            continue;
        }
        opcache_invalidate($script, true);
        $counter++;
    }

    echo 'Okey, ' . $counter . ' files flushed';
} else {
    echo "Invalid IP, {$ip}\n";
}
